/**
 * Created by Demon-pro on 2015/7/27.
 */
var Page = function () {
    function initReadonly() {
        $("#E6,#E5,#E7").prop("readonly", true);
    }

    function changeE18(e14) {
        switch (parseInt(e14)) {
            case 1:
                $("#E18").val(0.1);
                break;
            default :
                $("#E18").val(0.4);
        }
    }

    function checkboxHandle() {
        getCheckBoxUpdate();
        $("#E58").prop("readonly", true);
        $(":checkbox", ".form-body").click(function () {
            if ($(this).prop("checked")) {
                $("#E58").prop("readonly", false);
            } else {
                $("#E58").prop("readonly", true);
            }
        });

    }

    function e4Handle() {
        $("#E4").on("change", function () {
            initReadonly();
            switch (parseInt($(this).val())) {
                case 1:
                    $("#E7").prop("readonly", false);
                    getUpdate($(this));
                    break;
                case 2:
                    $("#E6,#E5").prop("readonly", false);
                    getUpdate($(this));
                    break;
            }
            changeE18($(this).val());
            getUpdate($("#E18"));
        }).change();
    }

    function popoverHandle() {
        $('#E32').popover({
            content: "<p>缓转弯头(R/b>0.5)请填写道体中心线弯曲半径R</p><p>急转弯头请填写道体内壁弯曲半径</p><p>内尖角弯头请填写‘0’</p>",
            title: "提示信息",
            placement: "top",
            html: true
        })
    }

    function getUpdate($obj) {
        validate.init($obj);
        if (validate.isNotNumber($obj.val())) {
            validate.invalid();
            return;
        }
        $.ajax({
            url: basePath + "tools/bend/updateParam",
            type: "post",
            dataType: "json",
            data: {
                paramName: $obj.attr("id"),
                paramValue: $obj.val()
            }
        }).success(function (){
            $.ajax({
                url:basePath + "toolBarData/add",
                type:"post",
                data:JSON.stringify({
                    calId:$("#currentId").val(),
                    calValue:toolBackFill.pageDataToString(":text,select",".form-body")
                }),
                dataType:"json",
                contentType:"application/json;charset=utf8"
            });
        });
        validate.valid()
    }
    function getCheckBoxUpdate() {
        var sChecked = 0.0;
        if( $("#isChecked").val() == 1){
            sChecked = 1.0;
        }else{
            sChecked = 0.0;
        }
        $.ajax({
            url: basePath + "tools/bend/updateParam",
            type: "post",
            dataType: "json",
            data: {
                paramName: "isChecked",
                paramValues: sChecked

                //paramValue: $("input[name=isChecked]:checked") ? 1 : 0
            }
        });
    }

    var update = function() {
        $("input").on("blur",function() {
            getUpdate($(this));
        })
    };

    var buttonHandle = function() {
        $("#calE15").on("click",function() {
            Metronic.blockUI({
                boxed:true,
                message:"数据计算中..."
            });
            $.get(basePath + "tools/bend/calE15",function(data) {
                $("#E15").val(data);
                Metronic.unblockUI();
            })
        });
        $("#cal").on("click",function() {
            Metronic.blockUI({
                boxed:true,
                message:"数据计算中..."
            });
            $.get(basePath + "tools/bend/cal",function(data) {
                $("#E19").val(data.e19);
                $("#E21").val(data.e21);
                $("#E35").val(data.e35);
                $("#E36").val(data.e36);
                Metronic.unblockUI();
            });
        });
    };

    return {
        init: function () {
            e4Handle();
            getCheckBoxUpdate();
            checkboxHandle();
            popoverHandle();
            getUpdate($("#E9"));
            getUpdate($("#E12"));
            getUpdate($("#E13"));
            getUpdate($("#E18"));
            window.onunload = function() {
                $.post(basePath + "tools/bend/clean",function() {});
            };
            update();
            buttonHandle();
        }
    }

}();